package ipebg.mzk.common.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class VerifyCode {
	
	public static final int TYPE_NUM_ONLY = 0;
	public static final int TYPE_LETTER_ONLY=1;
	public static final int TYPE_ALL_MIXED=2;
	public static final int TYPE_NUM_UPPER=3;
	public static final int TYPE_NUM_LOWER=4; 
	public static final int TYPE_UPPER_ONLY=5;
	public static final int TYPE_LOWER_ONLY=6;
	
	private VerifyCode(){}
	
	public static String generateTextCode(int type,int length,String exChars){
		if(length <= 0) return "";
		StringBuffer code = new StringBuffer();
		int i=0;
		Random r = new Random();
		switch(type){
		case TYPE_NUM_ONLY:
			while(i<length){
				int t = r.nextInt(10);
				if(exChars == null || exChars.indexOf(t+"")<0){
					code.append(t);
					i++;
				}
			}
			break;
		case TYPE_LETTER_ONLY:
			while(i<length){
				int t=r.nextInt(123);
				if((t>=97||(t>=65&&t<=90))&&(exChars==null||exChars.indexOf((char)t)<0)){
					code.append((char)t);
					i++;
				}
			}
			break;
		   case TYPE_ALL_MIXED:
	            while(i<length){
	                int t=r.nextInt(123);
	                if((t>=97||(t>=65&&t<=90)||(t>=48&&t<=57))&&(exChars==null||exChars.indexOf((char)t)<0)){
	                    code.append((char)t);
	                    i++;
	                }
	            }
	            break;    
	            
	        //数字、大写字母混合
	        case TYPE_NUM_UPPER:
	            while(i<length){
	                int t=r.nextInt(91);
	                if((t>=65||(t>=48&&t<=57))&&(exChars==null||exChars.indexOf((char)t)<0)){
	                    code.append((char)t);
	                    i++;
	                }
	            }
	            break;        
	            
	        //数字、小写字母混合
	        case TYPE_NUM_LOWER:
	            while(i<length){
	                int t=r.nextInt(123);
	                if((t>=97||(t>=48&&t<=57))&&(exChars==null||exChars.indexOf((char)t)<0)){
	                    code.append((char)t);
	                    i++;
	                }
	            }
	            break;        
	            
	        //仅大写字母
	        case TYPE_UPPER_ONLY:
	            while(i<length){
	                int t=r.nextInt(91);
	                if((t>=65)&&(exChars==null||exChars.indexOf((char)t)<0)){
	                    code.append((char)t);
	                    i++;
	                }
	            }
	            break;                
	            
	        //仅小写字母
	        case TYPE_LOWER_ONLY:
	            while(i<length){
	                int t=r.nextInt(123);
	                if((t>=97)&&(exChars==null||exChars.indexOf((char)t)<0)){
	                    code.append((char)t);
	                    i++;
	                }
	            }
	            break;                     
		}
		return code.toString();
	}
	public static BufferedImage generateImageCode(String textCode,int width,int height,int interLine,
			boolean randomLocation,Color backColor,Color foreColor,Color lineColor){
		BufferedImage bim = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
		Graphics g = bim.getGraphics();
		
		//画背景图
		g.setColor(backColor==null?getRandomColor():backColor);
		g.fillRect(0, 0, width, height);
		
		//画干扰线
		Random r = new Random();
		if(interLine>0){
			int x=0,y=0,x1=width,y1=0;
			for(int i=0;i<interLine;i++){
				g.setColor(lineColor == null?getRandomColor():lineColor);
				y=r.nextInt(height);
				y1 = r.nextInt(height);
				
				g.drawLine(x, y, x1, y1);
			}
		}
		
		int fsize = (int) (height*0.8);
		int fx = height=fsize;
		int fy = fsize;
		
		g.setFont(new Font(Font.DIALOG,Font.PLAIN,fsize));
		
		for(int i=0;i<textCode.length();i++){
            fy=randomLocation?(int)((Math.random()*0.3+0.9)*height):fy;//每个字符高低是否随机
            g.setColor(foreColor==null?getRandomColor():foreColor);
            g.drawString(textCode.charAt(i)+"",fx,fy);
            fx+=fsize*0.9;
		}
		 g.dispose();
	        
	     return bim;
		
	}
    private static Color getRandomColor(){
        Random r=new Random();
        Color c=new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
        return c;
    }
    
    public static BufferedImage generateImageCode(int type,int length,String exChars,int width,int height,int interLine,boolean randomLocation,Color backColor,Color foreColor,Color lineColor){
        
        String textCode=generateTextCode(type,length,exChars);
        BufferedImage bim=generateImageCode(textCode,width,height,interLine,randomLocation,backColor,foreColor,lineColor);
        
        return bim;
    }
    
}
